package com.sskj.finance.ui.activity;


import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bulong.rudeness.RudenessScreenHelper;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.shizhefei.view.coolrefreshview.SimpleOnPullListener;
import com.sskj.common.adapter.ItemClickSupport;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.common.util.ClickUtil;
import com.sskj.finance.R;
import com.sskj.finance.R2;
import com.sskj.finance.bean.AccountBean;
import com.sskj.finance.component.DaggerUserDataComponent;
import com.sskj.finance.presenter.TradeAccountActivityPresenter;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.CoinEnum;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.util.AppCircleCheckUtil;
import com.sskj.lib.util.CoinUtil;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

@Route(path = RConfig.FINAN_TRADE_ACCOUNT)//交易账户
public class TradeAccountActivity extends BaseActivity<TradeAccountActivityPresenter> {

    @BindView(R2.id.tvUSDT)
    TextView tvUSDT;
    @BindView(R2.id.tvRMB)
    TextView tvRMB;
    @BindView(R2.id.tvCardTitle)
    TextView tvCardTitle;
    @BindView(R2.id.tvWithdraw)
    TextView tvWithdraw;
    @BindView(R2.id.tvRecharge)
    TextView tvRecharge;
    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R2.id.coolRefreshView)
    CoolRefreshView coolRefreshView;
    @BindView(R2.id.llHead)
    LinearLayout llHead;
    @Autowired
    boolean isTrade;

    @Inject
    UserViewModel userViewModel;
    private SlimAdapter slimAdapter;
    private UserData userData;

    @Override
    protected int getLayoutId() {
        return R.layout.finan_activity_trade_account;
    }

    @Override
    public TradeAccountActivityPresenter getPresenter() {
        return new TradeAccountActivityPresenter();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mPresenter != null) {
            mPresenter.getData(isTrade);
        }
    }

    @Override
    protected void initView() {
        DaggerUserDataComponent.create().inject(this);
        ARouter.getInstance().inject(this);
        userViewModel.getUsers().observe(this,users->{
            if (users!=null&&users.size()>0){
                userData = users.get(0);
            }else {
                 userData=null;
            }
        });
        setTitle(isTrade ? App.INSTANCE.getString(R.string.finan_tradeAccountActivity1_j) : App.INSTANCE.getString(R.string.finan_tradeAccountActivity2_j));
        setRightTitle(App.INSTANCE.getString(R.string.finan_tradeAccountActivity3_j), v -> {
            if (isTrade) {
                ARouter.getInstance().build(RConfig.MINE_ASSET_RECORDS)
                        .navigation();
            } else {
                ARouter.getInstance().build(RConfig.FINAN_ACCOUNT_RECORD)
                        .withBoolean(Constans.IS_TRADE, isTrade)
                        .navigation();
            }
        });
        tvCardTitle.setText(isTrade ? App.INSTANCE.getString(R.string.finan_tradeAccountActivity1_j) : App.INSTANCE.getString(R.string.finan_tradeAccountActivity2_j));
        if (isTrade) {

            llHead.setBackground(ContextCompat.getDrawable(App.INSTANCE, R.mipmap.finan_bg_trade));
            ClickUtil.click(tvRecharge, () -> {
                ARouter.getInstance().build(RConfig.MINE_RECHARGE).navigation();
            });
            ClickUtil.click(tvWithdraw, () -> {
                if (!AppCircleCheckUtil.checkTradePwd(this,userData)){
                    return;
                }
                ARouter.getInstance().build(RConfig.MINE_WITHDRAW).navigation();

            });
        } else {
            llHead.setBackground(ContextCompat.getDrawable(App.INSTANCE, R.mipmap.finan_bg_finan));

            tvWithdraw.setVisibility(View.GONE);
            tvRecharge.setText(App.INSTANCE.getString(R.string.finan_transferActivity1_j));
            ClickUtil.click(tvRecharge, () -> {
                if (!AppCircleCheckUtil.checkTradePwd(this,userData)){
                    return;
                }
                ARouter.getInstance().build(RConfig.FINAN_TRANSFER).navigation();
            });
        }
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(this)
                .setPaintWidth((int) RudenessScreenHelper.pt2px(App.INSTANCE, 20))
                .setDividerColor(ContextCompat.getColor(App.INSTANCE, R.color.transparent))
        );
        slimAdapter = SlimAdapter.create().register(R.layout.finan_recy_item_trade_account, new SlimInjector<AccountBean.AssetBean>() {
            @Override
            public void onInject(AccountBean.AssetBean data, IViewInjector injector, List payloads) {
                injector
                        .image(R.id.ivCoin, CoinEnum.getByCode(data.getMark()).getRes())
                        .text(R.id.tvType, data.getMark())
                        .text(R.id.tvAvailable, data.getUsable())
                        .text(R.id.tvFreeze,  data.getFrost());
            }
        }).attachTo(recyclerView).updateData(new ArrayList());
        coolRefreshView.addOnPullListener(new SimpleOnPullListener() {
            @Override
            public void onRefreshing(CoolRefreshView refreshView) {
                mPresenter.getData(isTrade);
            }
        });

        ItemClickSupport.addTo(recyclerView)
                .setOnItemClickListener((recyclerView1, i, view) -> {
                    ARouter.getInstance().build(RConfig.FINAN_DETAIL)
                            .withString(Constans.PID, ((AccountBean.AssetBean) slimAdapter.getDataItem(i)).getPid())
                            .withString(Constans.CODE, ((AccountBean.AssetBean) slimAdapter.getDataItem(i)).getMark())
                            .withString(Constans.TYPE, isTrade ? "1" : "2")
                            .navigation();
                });
    }

    @Override
    protected void initData() {
        mPresenter.getData(isTrade);
    }

    public void updateUI(AccountBean data) {
        if (coolRefreshView.isRefreshing()) {
            coolRefreshView.setRefreshing(false);
        }
        tvUSDT.setText(CoinUtil.keepUSDT(data.getTtl_money()));
        tvRMB.setText(String.format("≈ %s TWD", CoinUtil.keepRMB(data.getTtl_cnymoney())));
        slimAdapter.updateData(data.getAsset());
    }
}
